//
//  MNDTopCycleView.m
//  miniwangdai2
//
//  Created by 谭伟 on 2017/4/5.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MNDTopCycleView.h"
#import "MNDWebViewController.h"
//#import <SCAdView.h>


#define ScrollImageURLStr @"https://m.miniwangdai.com/app/welcome/carousel_img"
@interface MNDTopCycleView ()<SDCycleScrollViewDelegate>
/* MNDHomeCycleModel */
@property (strong, nonatomic) NSArray<MNDHomeCycleModel *> *mndHomeCycleModelArray;
@property (strong, nonatomic) SDCycleScrollView *cycleScrollview;
@end
@implementation MNDTopCycleView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
//        [self setupCycleScrollView];
    }
    return self;
}

- (SDCycleScrollView *)cycleScrollview {
    if (!_cycleScrollview) {
        _cycleScrollview = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, self.bounds.size.height) delegate:self placeholderImage:[UIImage imageNamed:@"home_banner_placeholder"]];
        [self addSubview:_cycleScrollview];
        [_cycleScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(self);
        }];
        _cycleScrollview.placeholderImage = [UIImage imageNamed:@"home_banner_placeholder"];
        //设置pagecontrol的类型
        _cycleScrollview.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollview.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        //设置control图片
        UIImage *currentDotImage = [UIImage imageNamed:@"barnner_indicator_current"];
        _cycleScrollview.currentPageDotImage = currentDotImage;
        _cycleScrollview.pageDotImage = [UIImage imageNamed:@"barnner_indicator_default"];
//            pageControl.activeImage = [UIImage imageNamed:@"intro_current"];
//            pageControl.inactiveImage = [UIImage imageNamed:@"intro_default"];
        //间隔
        _cycleScrollview.autoScrollTimeInterval = 2.0;
//        _cycleScrollview.pageControlDotSize = currentDotImage.size;
    }
    return _cycleScrollview;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupCycleScrollView];
}


#pragma mark -轮播图

- (void)setupCycleScrollView {
    
    WeakSelf(self);
    [APIManager sendRequestWithPath:API_HOME_PICTURE parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            weakself.mndHomeCycleModelArray = [MNDHomeCycleModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"list_img"]];
            NSMutableArray *imagesURLStrings = [NSMutableArray array];
            for (MNDHomeCycleModel *model in weakself.mndHomeCycleModelArray) {
                //取到数据放入数组保存
                [imagesURLStrings addObject:model.img];
            }
            //设置轮播器的图片
            weakself.cycleScrollview.imageURLStringsGroup = imagesURLStrings;
        }
    } onFailure:^(NSError *error) {

    }];
}

#pragma mark -SDCycleScrollViewDelegate
//点击轮播图片
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (self.clickCycleImgBlock != nil) {
        
        self.clickCycleImgBlock(self.mndHomeCycleModelArray[index]);
    }
}


@end
